/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.pagebeans;

//import edu.chl.da.ejb.ctrl.IManageFlight;
import edu.chl.da.controllerBeans.ReservationBean;
import edu.chl.da.ejb.core.Destination;
import edu.chl.da.ejb.core.Flight;
import edu.chl.da.ejb.ctrl.IManageDestination;
import edu.chl.da.ejb.ctrl.IManageFlight;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;



@ManagedBean(name = "FlightBoxBean")
@SessionScoped
public class FlightBoxBean {

    private String fromDestination, toDestination;
    private ReservationBean reservation;

    private List<Destination> fromDestinations;
    private List<Destination> toDestinations;

    private List<SelectItem> listFromDestinations;
    private List<SelectItem> listToDestinations;

    @EJB
    private IManageFlight imf;

    @EJB
    private IManageDestination imd;

    /** Creates a new instance of ManageCustomersBean */
    public FlightBoxBean() {

    }

    public void load(){
        updateListFromDestinations();
        updateListToDestinations();

        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        ValueExpression ex =
           context.getApplication().getExpressionFactory().createValueExpression(elContext,"#{ReservationBean}", ReservationBean.class);

         this.reservation = (ReservationBean) ex.getValue(elContext);
    }

    private void updateListFromDestinations(){
        this.listFromDestinations = new ArrayList<SelectItem>();
        this.fromDestinations = imf.getAllUniqueFromDestinations();
        for(Destination d : fromDestinations){
            listFromDestinations.add(new SelectItem(d.getName(),d.getName()));
        }
    }

    private void updateListToDestinations(){
        this.toDestinations = new ArrayList<Destination>();
        if((fromDestination == null || fromDestination.equals("")) &&
           fromDestinations != null && fromDestinations.size() > 0){
            this.toDestinations.addAll(imf.getAllUniqueToDestinations(fromDestinations.get(0)));
        }
        else{
            this.toDestinations.addAll(imf.getAllUniqueToDestinations(imd.getDestinationByName(fromDestination)));
        }

        this.listToDestinations = new ArrayList<SelectItem>();

        for(Destination d : toDestinations){
            listToDestinations.add(new SelectItem(d.getName(),d.getName()));
        }
    }

    public String getFromDestination() {
        return fromDestination;
    }

    public void setFromDestination(String fromDestination) {
        this.fromDestination = fromDestination;
        this.reservation.setFromDestination(fromDestination);
    }

    public List<Destination> getFromDestinations() {
        return fromDestinations;
    }

    public void setFromDestinations(List<Destination> fromDestinations) {
        this.fromDestinations = fromDestinations;
    }

    public List<SelectItem> getListFromDestinations() {
        return listFromDestinations;
    }

    public void setListFromDestinations(List<SelectItem> listFromDestinations) {
        this.listFromDestinations = listFromDestinations;
    }

    public List<SelectItem> getListToDestinations() {
        return listToDestinations;
    }

    public void setListToDestinations(List<SelectItem> listToDestinations) {
        this.listToDestinations = listToDestinations;
    }

    public String getToDestination() {
        return toDestination;
    }

    public void setToDestination(String toDestination) {
        this.toDestination = toDestination;
        
        if(this.reservation != null)
            this.reservation.setToDestination(toDestination);
    }

    public List<Destination> getToDestinations() {
        return toDestinations;
    }

    public void setToDestinations(List<Destination> toDestinations) {
        this.toDestinations = toDestinations;
    }
}

